package com.simin.base.sqlite;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.UiThread;

import java.util.Set;

/**
 * Created by wsm on 2024/10/21.
 * <p>
 */
public class DiskCacheManager {

    private static final String DEFAULT_NAME_SP = "sp_disk_cache";

    private static Context sContext;
    private static SharedPreferences sSp;

    public static final String TOKEN = "token";//登录token
    public static final String LOGIN_INFO = "loginInfo"; //登录信息
    public static final String USER_INFO = "userInfo"; //登录用户信息
    public static final String LOGIN_STATUS = "login_status"; //登录状态
    public static final String SEARCH_HISTORY = "search_history";//搜索记录

    @UiThread
    public static void initialize(Context context) {
        sContext = context.getApplicationContext();
        sSp = sContext.getSharedPreferences(DEFAULT_NAME_SP, Context.MODE_PRIVATE);
    }

    /**
     * 只支持 sp 查询
     *
     * @param key
     * @return 是否存在改 key
     */
    public static boolean contains(String key) {
        return sSp.contains(key);
    }

    public static void putString(String key, String value) {
        sSp.edit().putString(key, value).apply();
    }

    public static String getString(String key) {
        return sSp.getString(key, null);
    }

    public static String getString(String key, String defaultValue) {
        return sSp.getString(key, defaultValue);
    }

    public static void putLong(String key, long value) {
        sSp.edit().putLong(key, value).apply();
    }

    public static long getLong(String key) {
        return sSp.getLong(key, 0L);
    }

    public static long getLong(String key, long defaultValue) {
        return sSp.getLong(key, defaultValue);
    }

    public static void putBoolean(String key, boolean value) {
        sSp.edit().putBoolean(key, value).apply();
    }

    public static Boolean getBoolean(String key) {
        return sSp.getBoolean(key, false);
    }

    public static Boolean getBoolean(String key, Boolean defaultValue) {
        return sSp.getBoolean(key, defaultValue);
    }

    public static void putInt(String key, int value) {
        sSp.edit().putInt(key, value).apply();
    }

    public static int getInt(String key) {
        return sSp.getInt(key, 0);
    }

    public static int getInt(String key, int defaultValue) {
        return sSp.getInt(key, defaultValue);
    }

    public static boolean enable(String key) {
        return sSp.getInt(key, 0) > 0;
    }

    public static void putFloat(String key, float value) {
        sSp.edit().putFloat(key, value).apply();
    }

    public static float getFloat(String key) {
        return sSp.getFloat(key, 0f);
    }

    public static float getFloat(String key, float defaultValue) {
        return sSp.getFloat(key, defaultValue);
    }

    public static void putStringSet(String key, Set<String> value) {
        sSp.edit().putStringSet(key, value).apply();
    }

    public static Set<String> getStringSet(String key) {
        return sSp.getStringSet(key, null);
    }

    public static Set<String> getStringSet(String key, Set<String> defaultValue) {
        return sSp.getStringSet(key, defaultValue);
    }

    //清空所有数据
    public static void clearAll() {
        putString(TOKEN, "");
        putString(LOGIN_INFO, "");
        putString(USER_INFO, "");
        putBoolean(LOGIN_STATUS, false);
    }
}
